package yto.freemarker.java8.dateapi;

import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * java8的时间api的类
 *
 * @author dongxing
 * @create 2018-06-23 18:07
 **/
public class Java8DateApi {

    /**
     * LocalDateTime
     */
    @Test
    public void test1() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        System.out.println("-------------");
        LocalDateTime of = LocalDateTime.of(2017, 2, 4, 4, 3, 2);
        System.out.println(of);
        System.out.println("-----------------");

        //加2年
        LocalDateTime localDateTime1 = localDateTime.plusYears(2);
        System.out.println(localDateTime1);

        //减2月
        LocalDateTime localDateTime2 = localDateTime.minusMonths(2);
        System.out.println(localDateTime2);
        System.out.println("-----------------------------");

        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());


        System.out.println("------------------");
        //格式化日期的格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(format);


    }

    /**
     * Instant:时间戳（以unix元年：  1970年1月1号 00:00:00 到某个时间之间的毫秒值）
     */
    @Test
    public void test2() {
        Instant now = Instant.now();  //默认获取的UTC时区
        System.out.println(now);
        System.out.println("------------------");

        //带偏移量的计算
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        System.out.println("------------------");

        System.out.println(now.toEpochMilli());
        System.out.println("------------------");

        Instant milli = Instant.ofEpochSecond(60);
        System.out.println(milli);
    }

    /**
     * duration  ：计算两个时间之间的间隔
     * <p>
     * period：  计算两个日期之间的间隔
     */
    @Test
    public void test3() throws InterruptedException {
        Instant start = Instant.now();
        Thread.sleep(2000);
        Instant end = Instant.now();

        System.out.println(Duration.between(start, end).toMillis());
        System.out.println("--------------------------");

        LocalDateTime localDateTime = LocalDateTime.now();
        Thread.sleep(2000);
        LocalDateTime now = LocalDateTime.now();
        System.out.println(Duration.between(localDateTime, now).getSeconds());
        System.out.println("--------------------------");


    }

    /**
     * period 计算两个日期之间的值
     */
    @Test
    public void test4() {
        LocalDate of = LocalDate.of(2015, 1, 1);
        LocalDate now = LocalDate.now();
        System.out.println(Period.between(of, now).getDays());
    }

    /**
     * 时间矫正器:TemporalAdjuster
     * <p>
     * 工具类：TemporalAdjusters
     */
    @Test
    public void test5() {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        System.out.println("----------------");
        //将当月的天数变为10号
        LocalDateTime localDateTime = now.withDayOfMonth(10);
        System.out.println(localDateTime);
        System.out.println("----------------");

        LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println(with);
        System.out.println("---------------------");


        //自定义的工作日
        now.with((e) -> {
            LocalDateTime localDateTime1 = (LocalDateTime) e;
            DayOfWeek dayOfWeek = localDateTime1.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return localDateTime1.plusDays(3);
            } else {
                return localDateTime1.plusDays(2);
            }

        });

    }


    /**
     * 格式化日期及转换回来
     */

    @Test
    public void test6() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(dateTimeFormatter);
        System.out.println(format);

        System.out.println("---------------------------------");
        //格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format1 = formatter.format(now);
        System.out.println(format1);
        System.out.println("---------------------------------");

        //解析成原来的样子
        LocalDateTime parse = LocalDateTime.parse(format, DateTimeFormatter.ISO_DATE_TIME);
        System.out.println(parse);


    }


    /**
     * 带时区的日期转换
     */
    @Test
    public void test7() {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);

        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
        System.out.println(now);

    }


}
